<?php
include_once(dirname(__FILE__)."/../inc/common.php");

$accountClass = new tally_account();
$accountListTemp = $accountClass->findAccountList($userId, true);
$accountList = array();
foreach($accountListTemp as $account){
    $accountList[$account["id"]] = $account;
}

$tallyFlowTypeClass = new tally_flowType();
$flowTypeListTemp = $tallyFlowTypeClass->findAllFlowType($userId, true);
$flowTypeList = array();
foreach($flowTypeListTemp as $flowType){
    $flowTypeList[$flowType["id"]] = $flowType;
}

$tallyTagClass = new tally_tag();
$tagListTemp = $tallyTagClass->findTagByAccountId($userId, null, true, 0, -1);
$tagList = array();
foreach($tagListTemp as $tag){
    $tagList[$tag["id"]] = $tag;
}

$dateFrom = isset($_POST["dateFrom"])?$_POST["dateFrom"]:null;
$dateTo = isset($_POST["dateTo"])?$_POST["dateTo"]:null;
$orderField = isset($_POST["orderField"])?$_POST["orderField"]:"";
$tagIds = isset($_POST["tagId"])?$_POST["tagId"]:"";
$remark = isset($_POST["remark"])?$_POST["remark"]:"";
$minAmount = isset($_POST["minAmount"])?$_POST["minAmount"]:null;
$maxAmount = isset($_POST["maxAmount"])?$_POST["maxAmount"]:null;
$curPage = isset($_GET["curPage"])?$_GET["curPage"]:1;
$pageSize = 50;
$accountId = isset($_POST["accountId"])?$_POST["accountId"]:null;
$categoryId = isset($_GET["oid"])?$_GET["oid"]:null;

$flowClass = new tally_flow();
$flowList = $flowClass->findFlowByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
    $tagIds, $remark, $minAmount, $maxAmount, $orderField, ($curPage-1)*$pageSize, $pageSize);
$total = $flowClass->countFlowByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
    $tagIds, $remark, $minAmount, $maxAmount);
$nownum = $flowList?count($flowList):0;

$inCount = $flowClass->inCountByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
    $tagIds, $remark, $minAmount, $maxAmount, $orderField);
$outCount = $flowClass->outCountByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
    $tagIds, $remark, $minAmount, $maxAmount, $orderField);
$inOutDiff = $inCount-$outCount;
?>
<!--begin:数据列表-------------------------------------------------------------------------------------------------->
<table id="reportDetailList" class="listtable" cellpadding="0" cellspacing="1">
    <thead>
    <tr>
        <th width="30%">发生日期</th>
        <th width="30%">标签</th>
        <th width="15%">金额</th>
        <th width="25%">说明</th>
    </tr>
    </thead>

    <tbody>
    <?php
    foreach($flowList as $flow) {
        $inout = "left";
        if ($flow["moneyOut"] > 0) {
            $inout = "right";
        }

        $flowType = "";
        if (tally_flow::$INOUT_TRANSFER != $flow["inOrOut"]) {
            $flowType = isset($flowTypeList[$flow["flowType"]]) ? $flowTypeList[$flow["flowType"]]["name"] : '';
        } else {
            $flowType = $accountList[$flow["transferTargetAccountId"]]["name"];
        }

        $bizType = 0;
        switch ($flow["inOrOut"]) {
            case tally_flow::$INOUT_INCOMME:
                $bizType = 11;
                break;
            case tally_flow::$INOUT_EXPENSE:
                $bizType = 20;
                break;
            default:
                $bizType = 30;
                break;

        }

        $tradeTime = strtotime($flow["tradeTime"]);
        $mark = "";
        $tagIdList = $flowClass->findTagByFlow($userId, $flow["id"]);
        if ($tagIdList) {
            $tags = "";
            foreach ($tagIdList as $tagMap) {
                if ($tags) {
                    $tags .= ",";
                }
                $tags .= $tagList[$tagMap["tagId"]]["name"];
            }
            if ($tags) {
                $mark .= "[" . $tags . "]";
            }
        }
        if ($flow["desc"]) {
            $mark .= $flow["desc"];
        }
        ?>
        <tr>
            <td>
                <p title="<?php echo date("Y-m-d H:i", $tradeTime)?>">
                    <?php echo date("Y-m-d", $tradeTime)?>
                </p>
            </td>
            <td>
                <?php echo $mark?>
            </td>

            <td class="number">
                <?php echo $flow["moneyIn"]>0?$flow["moneyIn"]:$flow["moneyOut"]?>
            </td>


            <td></td>
        </tr>
        <?php
    }
    ?>

    </tbody>
    <tfoot>
    <tr>
        <td colspan="10"><span class=pagebar><?php echo isspage($total,$nownum,$pageSize,$curPage); ?></span></td>
    </tr>
    </tfoot>
</table>

<div class="buttons">
    <input type="button" class="button"  value="关闭" onclick="CM.close(this);return false;"/>
</div>
<!--end:数据列表区-------------------------------------------------------------------------------------------------->
